export const openPosition = ()=>{
    let system = uni.getSystemInfoSync()
    if(system.platform === 'android'){//判断平台
        var context = plus.android.importClass("android.content.Context")
        var locationManager = plus.android.importClass("android.location.LocationManager")
        var main = plus.android.runtimeMainActivity()
        var mainSvr = main.getSystemService(context.LOCATION_SERVICE)
        console.log('GPS',mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER))
        if(!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)){
            uni.showModal({
                title:'提示',
                content:'请打开定位服务功能',
                showCancel:false,
                success() {
                    if(!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)){
                        let main = plus.android.runtimeMainActivity();
                        let Intent = plus.android.importClass("android.content.Intent");
                        let mIntent = new Intent('android.settings.ACTION_SETTINGS');
                        main.startActivity(mIntent);//打开系统设置GPS服务页面 
                    }else{
                        uni.showToast({
                            title:'定位功能已启动',
                            duration:2000
                        })
                    }
                }
            })
        }
    }
}

// 高德坐标解析
export const posToCity = async (location, extensions = 0) => new Promise((resolve, reject) => {
  uni.request({
    url: `https://restapi.amap.com/v3/geocode/regeo`,
    method: 'GET',
    data: {
      key: '47fa3bcc499016104283079c04177447',
      location: `${location.longitude},${location.latitude}`,
      extensions: extensions ? 'all' : 'base'
    },
    success: ({data}) => {
		console.log(data, location)
      const {status, info, regeocode} = data
      if (status === '1') {
        // console.log(regeocode)
        if ('pois' in regeocode) {
          regeocode.pois = regeocode.pois.filter(item => typeof item.address === 'string' && typeof item.location === 'string').map(item => ({
            id: item.id,
            title: item.name,
            address: item.address,
            location: {
              lat: item.location.split(',')[1],
              lng: item.location.split(',')[0]
            }
          }))
        }
        resolve(regeocode)
      } else {
        reject(info)
      }
    },
    fail: err => {
      reject(err)
    }
  })
})